<?php
/**
 * $Id: tags.php 44 2010-11-12 06:50:01Z varun $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * @version 			$Rev: 44 $
 * @lastModifiedBy		$LastChangedBy: varun $
 * @lastChanged			$Date: 2010-11-12 12:20:01 +0530 (Fri, 12 Nov 2010) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */
App::import('Helper',"Tags.AbstractTags");
class TagsHelper extends AbstractTagsHelper {

	var $helpers = array('Html');

	var $baseUrl = null;

	var $cloudCssPath ='/tags/css/tag_cloud.css';

	/**
	 * Show list of tags with delimiter
	 *
	 * @param unknown_type $tags
	 * @param unknown_type $type
	 * @param unknown_type $delimiter
	 * @param unknown_type $showLink
	 * @return unknown
	 */
	function show($tags, $type, $delimiter=', ',$showLink=false)
	{
		if(empty($tags))
		{
			return null;
		}
		$outString = '';
		foreach($tags as $tag)
		{
			if($showLink)			
			$tag['tag'] = $this->getLink($tag, $type);
			$outString .= $tag['tag'].$delimiter;
		}

		// to remove un-necessary delimiter from end
		$endChars = (-1)*strlen($delimiter);
		$outString = substr($outString, 0, $endChars);
		return $outString;
	}

	function getLink($tag, $type)
	{
		$options = array('class'=>'tag');
		if($this->baseUrl)
			return $this->Html->link($tag['tag'], $this->baseUrl.$tag['slug'], $options);
		else
			return $this->Html->link($tag['tag'], '/tags/index/'.$type.'/'.$tag['slug'], $options);
	}

	function genrateCloud($type=null, $link='', $max_size=32, $min_size=12, $limit=0, $include_default_css = true)
	{
		$tagData = ClassRegistry::init('Tags.TaggedItem')->getTagCount(@$type,true);
		if(empty($tagData))
		{
			return false;
		}
		extract($tagData);
		$out = '';
		if($include_default_css == true)
		{
			$out .= $this->Html->css($this->cloudCssPath);
		}
		$max_qty=$min_qty=0;
		if(!empty($tags))
		{	
			$max_qty = max(array_values($tags));
			$min_qty = min(array_values($tags));
		}	
		$spread = $max_qty - $min_qty;
		 if ($spread == 0) 
			$spread = 1; 
		$step = ($max_size - $min_size) / ($spread);
		$counter=1;
		$template = '[[tag]]';
		foreach ($tags as $name => $value)
		{
			if(isset($slugs[$name]))
					$replace = $slugs[$name];
				else
					$replace = $name;
			$url = '#';
			if(is_string($link))
			{
				if(stripos($link,$template)!==false)
				{
					$url = str_ireplace('[[tag]]',$replace,$link);		
				}	
			}
			else if(is_array($link))
			{
				$url  = $link;
				$key = array_search($template,$url);
				if($key!==false)
				{
					$url[$key] = $replace; 
				}
			}
			$size = round($min_size + (($value - $min_qty) * $step));
			$out .= $this->Html->link($name,$url,array('class'=>'tag_cloud','style'=>'font-size:'.$size.'px')).' ';
			if($counter==$limit)
				break;
			$counter++;
		}
		return  $out;

	}

}
?>